package cn.nexd.utils;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.io.IOException;

import cn.nexd.utils.framework.tools.FileUtil;
import cn.nexd.utils.framework.tools.Logger;
import cn.nexd.utils.framework.tools.SDCardUtil;

/**
 * Created by wangxu on 16/12/27.
 */
public class FileActivity extends AppCompatActivity {

    private static final String TAG = "FileActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file);
    }


    public void deleteFile(View view) {
        String path = SDCardUtil.getSDCardPath() + "/nexd/collect/20161227/";
        try {
            FileUtil.deleteFile(path);
            Logger.debug(TAG, "deleteFile: 删除成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void compressFile(View view) {
        compressFile();
    }

    void compressFile() {

        final String srcFilePath = SDCardUtil.getSDCardPath() + "/nexd/collect/20161102/YouTangGuangChang/";

        final String targetFilePath = SDCardUtil.getSDCardPath() + "/nexd/collect/YouTangGuangChang.zip";

        Log.d(TAG, "compressFile: \n" +
                "srcFilePath = " + srcFilePath + "\n" +
                "targetFilePath = " + targetFilePath);


        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Log.d(TAG, "run: 开始压缩文件");
                    // 使用  Apache 文件压缩工具 处理。


//                    FileUtil.archiveCompressFile(new File(srcFilePath), targetFilePath);

//                    ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(new File(targetFilePath)));

//                    ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(new FileOutputStream(targetFilePath));
//
//
//                    long start = System.currentTimeMillis();
//                    compressFolderChangeToZip(new File(srcFilePath), zipArchiveOutputStream, "");
//
//                    long end = System.currentTimeMillis();
//
//                    Log.d(TAG, "run: finished , time = " + (end - start));
//                    compressFloderChangeToZip(new File(srcFilePath), zipOutputStream, "");

//                    zipOutputStream.close();
//                    zipArchiveOutputStream.closeArchiveEntry();
//                    zipArchiveOutputStream.close();

//                    FileUtil.compressFile(new File(srcFilePath), targetFilePath);
                    Log.d(TAG, "run: 文件压缩完成");
                    showToast("压缩完成");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }

    void showToast(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(FileActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void unCompressFile(View view) {
        String zipPath = SDCardUtil.getSDCardPath() + "/nexdLocation/location/download/10225026.zip";
        String unZipPath = SDCardUtil.getSDCardPath() + "/nexdLocation/location/";

        try {
            FileUtil.unCompressZipFile(zipPath, unZipPath);
            Log.d(TAG, "unCompressFile: 解压完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
